package oop.controller.action.exam;

import java.sql.SQLException;
import java.util.Date;

import oop.controller.action.DefaultAction;
import oop.data.Examination;
import oop.db.dao.ExaminationDAO;

import com.oreilly.servlet.ParameterNotFoundException;

public class CreateAction extends DefaultAction {

	@Override
	public void performImpl() throws Exception {
		title("Tạo kì thi mới");
		try {
			if ("create".equals(request.getParameter("ec_submit"))) {
				String name = request.getParameter("ec_name");
				long teacherId = getUser().getId();
				long testId = getParams().getLong("ec_testid");
				Date date = new Date();
				String clazz = request.getParameter("ec_class");

				Examination examination = ExaminationDAO.create(teacherId,
						testId, date, name, clazz);
				setNextAction("mark.create&mc_exam=" + examination.getId());
				return;
			}
		} catch (NumberFormatException ex) {
			error("id không hợp lệ");
		} catch (SQLException ex) {
			// XXX trùng tên
			// XXX id sai
			throw ex;
		} catch (ParameterNotFoundException ex) {
			error("Thiếu thông tin");
		}
	}

}
